<!DOCTYPE html>
<html lang="">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="由于项目中使用到这方面的适配，所以这里做一个整理以便后面适用。"/>




  <meta name="keywords" content="适配, ZOE码坊" />










  <link rel="alternate" href="/blog/default" title="ZOE码坊">




  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=2.9.0" />



<link rel="canonical" href="https://luys.gitee.io/blog/2018/08/29/Android沉浸式状态栏整理/"/>



  <link rel="stylesheet" type="text/css" href="/blog/lib/fancybox/jquery.fancybox.css" />




  <link rel="stylesheet" type="text/css" href="/blog/lib/nprogress/nprogress.min.css" />



<link rel="stylesheet" type="text/css" href="/blog/css/style.css?v=2.9.0" />



  



  <script id="baidu_push">
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>





  <script src="//cdn1.lncld.net/static/js/3.1.1/av-min.js"></script>
  <script id="leancloud">
    AV.init({
      appId: "kdb8x3rBNollDwJ8ulzKQPb3-gzGzoHsz",
      appKey: "dRlqoI7xVfUXI5urx5r6uOYC"
    });
  </script>




<script>
  window.config = {"title":"ZOE码坊","subtitle":null,"description":null,"author":"Sam Lu","language":null,"timezone":null,"url":"https://luys.gitee.io/blog","root":"/blog/","permalink":":year/:month/:day/:title/","permalink_defaults":null,"source_dir":"source","public_dir":"public/blog","tag_dir":"tags","archive_dir":"archives","category_dir":"categories","code_dir":"downloads/code","i18n_dir":":lang","skip_render":null,"new_post_name":":title.md","default_layout":"post","titlecase":false,"external_link":true,"filename_case":0,"render_drafts":false,"post_asset_folder":false,"relative_link":false,"future":true,"highlight":{"enable":true,"auto_detect":false,"line_number":true,"tab_replace":null},"default_category":"uncategorized","category_map":null,"tag_map":null,"date_format":"YYYY-MM-DD","time_format":"HH:mm:ss","per_page":10,"pagination_dir":"page","theme":"even","deploy":{"type":"git","repo":"https://gitee.com/luys/blog.git","branch":"master"},"ignore":[],"keywords":null,"index_generator":{"per_page":10,"order_by":"-date","path":""},"category_generator":{"per_page":10},"tag_generator":{"per_page":10},"archive_generator":{"per_page":10,"yearly":true,"monthly":true,"daily":false},"marked":{"gfm":true,"pedantic":false,"sanitize":false,"tables":true,"breaks":true,"smartLists":true,"smartypants":true,"modifyAnchors":"","autolink":true},"server":{"port":4000,"log":false,"compress":false,"header":true},"since":2015,"favicon":"/favicon.ico","rss":"default","menu":{"Home":"/","Tags":"/tags","Categories":"/categories"},"color":"Cobalt Blue","toc":true,"fancybox":true,"pjax":true,"copyright":{"enable":true,"license":"<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-nc/4.0/\" target=\"_blank\">知识共享署名-非商业性使用 4.0 国际许可协议</a>"},"reward":{"enable":false,"qrCode":{"wechat":null,"alipay":null}},"social":{"email":"ltym_lys@126.com","stack-overflow":null,"twitter":null,"facebook":null,"linkedin":null,"google":null,"github":"https://github.com/ltym2016","weibo":null,"zhihu":null,"douban":null,"pocket":null,"tumblr":null,"instagram":null},"leancloud":{"app_id":"kdb8x3rBNollDwJ8ulzKQPb3-gzGzoHsz","app_key":"dRlqoI7xVfUXI5urx5r6uOYC"},"baidu_analytics":null,"baidu_verification":null,"google_analytics":null,"google_verification":null,"disqus_shortname":null,"changyan":{"appid":null,"appkey":null},"livere_datauid":null,"version":"2.9.0"};
</script>

    <title> Android沉浸式状态栏整理 - ZOE码坊 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/blog/." class="logo">ZOE码坊</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/blog/">
        <li class="mobile-menu-item">
          
          
            首页
          
        </li>
      </a>
    
      <a href="/blog/tags">
        <li class="mobile-menu-item">
          
          
            标签
          
        </li>
      </a>
    
      <a href="/blog/categories">
        <li class="mobile-menu-item">
          
          
            分类
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/blog/." class="logo">ZOE码坊</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/blog/">
            
            
              首页
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/blog/tags">
            
            
              标签
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/blog/categories">
            
            
              分类
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          Android沉浸式状态栏整理
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2018-08-29
        </span>
        
          <div class="post-category">
            
              <a href="/blog/categories/Android/">Android</a>
            
          </div>
        
        
        <div class="post-visits"
             data-url="/blog/2018/08/29/Android沉浸式状态栏整理/"
             data-title="Android沉浸式状态栏整理">
            阅读次数 0
          </div>
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#StatusBarUtils"><span class="toc-text">StatusBarUtils</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Android-沉浸式状态栏适配"><span class="toc-text">Android 沉浸式状态栏适配</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Android-4-4之前"><span class="toc-text">Android 4.4之前</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Android-4-4到5-0-不包括5-0-并且非miui和非Flyme系统"><span class="toc-text">Android 4.4到5.0(不包括5.0)并且非miui和非Flyme系统</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Android-5-x系列并且非miui和非Flyme系统"><span class="toc-text">Android 5.x系列并且非miui和非Flyme系统</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Android-4-4以上的MIUI和Flyme以及Android6-0以上系统"><span class="toc-text">Android  4.4以上的MIUI和Flyme以及Android6.0以上系统</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#StatusBarUtils如何使用"><span class="toc-text">StatusBarUtils如何使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#步骤一"><span class="toc-text">步骤一</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#步骤二"><span class="toc-text">步骤二</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#步骤三"><span class="toc-text">步骤三</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#步骤四"><span class="toc-text">步骤四</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Thanks"><span class="toc-text">Thanks</span></a></li></ol></li></ol></li></ol>
    </div>
  </div>



    <div class="post-content">
      
        <h1 id="StatusBarUtils"><a href="#StatusBarUtils" class="headerlink" title="StatusBarUtils"></a>StatusBarUtils</h1><h2 id="Android-沉浸式状态栏适配"><a href="#Android-沉浸式状态栏适配" class="headerlink" title="Android 沉浸式状态栏适配"></a>Android 沉浸式状态栏适配</h2><h4 id="Android-4-4之前"><a href="#Android-4-4之前" class="headerlink" title="Android 4.4之前"></a>Android 4.4之前</h4><p>在Android系统4.4以前，状态栏的背景色和字体颜色都是不能改变的。默认为黑色。</p>
<h4 id="Android-4-4到5-0-不包括5-0-并且非miui和非Flyme系统"><a href="#Android-4-4到5-0-不包括5-0-并且非miui和非Flyme系统" class="headerlink" title="Android 4.4到5.0(不包括5.0)并且非miui和非Flyme系统"></a>Android 4.4到5.0(不包括5.0)并且非miui和非Flyme系统</h4><p>在Android系统4.4到5.0(不包括5.0)并且非miui和非Flyme系统，状态栏的背景和字体颜色由系统决定，这里不做兼容。正常有下面两种样式。</p>
<h4 id="Android-5-x系列并且非miui和非Flyme系统"><a href="#Android-5-x系列并且非miui和非Flyme系统" class="headerlink" title="Android 5.x系列并且非miui和非Flyme系统"></a>Android 5.x系列并且非miui和非Flyme系统</h4><ul>
<li>在Android5.系列的系统上，可以设置状态栏的颜色，但不可以改变状态栏字体和图标的颜色。所以这里统一设置为半透明效果，可以通过setStatusBarColor设置状态栏的背景色，但是部分华为机型不支持setStatusBarColor的方式，只能是黑色，至少保证状态栏图标可见。</li>
<li>部分华为机型不支持setStatusBarColor的方式，只能是黑色</li>
<li>这里如果想设置状态栏的颜色和标题栏颜色一样，可以在标题栏上方放一个View，动态的设置view的高度为状态栏的高度。</li>
</ul>
<h4 id="Android-4-4以上的MIUI和Flyme以及Android6-0以上系统"><a href="#Android-4-4以上的MIUI和Flyme以及Android6-0以上系统" class="headerlink" title="Android  4.4以上的MIUI和Flyme以及Android6.0以上系统"></a>Android  4.4以上的MIUI和Flyme以及Android6.0以上系统</h4><ul>
<li>4.4以上的MIUI和Flyme系统以及Android6.0以上系统机型适配沉浸式状态栏，可以通过StatusBarUtils里面相关方法设置状态栏字体和图标的颜色切换</li>
</ul>
<h3 id="StatusBarUtils如何使用"><a href="#StatusBarUtils如何使用" class="headerlink" title="StatusBarUtils如何使用"></a>StatusBarUtils如何使用</h3><h4 id="步骤一"><a href="#步骤一" class="headerlink" title="步骤一"></a>步骤一</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">implementation &apos;com.samlu:statusbarutils:1.0.1&apos;</span><br></pre></td></tr></table></figure>
<h4 id="步骤二"><a href="#步骤二" class="headerlink" title="步骤二"></a>步骤二</h4><ul>
<li><p>直接在Activity中设置Window属性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">protected void onCreate(Bundle savedInstanceState) &#123;</span><br><span class="line">   if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT) &#123;</span><br><span class="line">        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);</span><br><span class="line">    &#125;</span><br><span class="line">    super.onCreate(savedInstanceState);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>或者在xml的style文件中设置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;item name=&quot;android:windowTranslucentStatus&quot;&gt;true&lt;/item&gt; </span><br><span class="line">android:fitsSystemWindows=&quot;true&quot;</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用android:windowTranslucentStatus属性需要在res目录下新建values-v19文件夹，style文件要放在里面</p>
</li>
</ul>
<h4 id="步骤三"><a href="#步骤三" class="headerlink" title="步骤三"></a>步骤三</h4><ul>
<li>在BaseActivity的onCreate里面调用StatusBarUtils.transparencyBar(this);开启沉浸式状态栏</li>
<li>1.0.1版本增加支持类似支付宝蒙层效果，这样就不需要改变状态栏的字体和图标的颜色。<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">// isOpen=true开启，false关闭，默认关闭</span><br><span class="line">StatusBarUtils.transparencyBar(Activity activity, boolean isOpen)</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line">  * 开启沉浸式状态栏</span><br><span class="line">  *</span><br><span class="line">  * @param activity</span><br><span class="line">  * @param isOpen 是否开启蒙层效果的状态栏（在5.0以上机型适用）</span><br><span class="line">  */</span><br><span class="line"> public static void transparencyBar(Activity activity, boolean isOpen) &#123;</span><br><span class="line">     isOpenMonLayer = isOpen;</span><br><span class="line">     transparencyBar(activity,0x40000000);</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h4 id="步骤四"><a href="#步骤四" class="headerlink" title="步骤四"></a>步骤四</h4><ul>
<li><p>在需要设置状态栏黑白字体的Activity里面调用如下方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">// 设置状态栏黑色字体图标 适配4.4以上版本的MIUI、Flyme和6.0以上版本其他Android</span><br><span class="line">StatusBarUtils.StatusBarIconDark(Activity activity)</span><br><span class="line">// 设置状态栏白色字体图标 适配4.4以上版本的MIUI、Flyme和6.0以上版本其他Android</span><br><span class="line">StatusBarUtils.StatusBarIconLight(Activity activity)</span><br></pre></td></tr></table></figure>
</li>
<li><p>如果StatusBarUtils.transparencyBar(Activity activity, boolean isOpen)的isOpen设置为true，上面的方面可以不调用。</p>
<h3 id="Thanks"><a href="#Thanks" class="headerlink" title="Thanks"></a>Thanks</h3><p><a href="https://github.com/QMUI/QMUI_Android/tree/1390e3a32941fb6f5cef4b1c815e2a6409902047" target="_blank" rel="noopener">QMUI</a></p>
</li>
</ul>

      
    </div>

    
      
      

  <div class="post-copyright">
    <p class="copyright-item">
      <span>原文作者: </span>
      <a href="https://luys.gitee.io/blog">Sam Lu</a>
    </p>
    <p class="copyright-item">
      <span>原文链接: </span>
      <a href="https://luys.gitee.io/blog/2018/08/29/Android沉浸式状态栏整理/">https://luys.gitee.io/blog/2018/08/29/Android沉浸式状态栏整理/</a>
    </p>
    <p class="copyright-item">
      <span>许可协议: </span>
      
      <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>
    </p>
  </div>



      
      
  <div class="post-reward">
    <input type="checkbox" name="reward" id="reward" hidden />
    <label class="reward-button" for="reward">赞赏支持</label>
    <div class="qr-code">
      
      
      
    </div>
  </div>

    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/blog/tags/适配/">适配</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/blog/2018/08/29/Android开发与艺术探索/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">Android开发与艺术探索</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
    
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
    
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="mailto:ltym_lys@126.com" class="iconfont icon-email" title="email"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
        
          <a href="https://github.com/ltym2016" class="iconfont icon-github" title="github"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
    
    
    
      
      <a href="/blog/atom.xml" class="iconfont icon-rss" title="rss"></a>
    
  </div>


<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://hexo.io/">Hexo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/ahonn/hexo-theme-even">Even</a>
  </span>

  <span class="copyright-year">
    
    &copy; 
     
      2015 - 
    
    2018

    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">Sam Lu</span>
  </span>
</div>

      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  

  



    
  



  
  





  
    <script type="text/javascript" src="/blog/lib/jquery/jquery.min.js"></script>
  

  
    <script type="text/javascript" src="/blog/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/blog/lib/fancybox/jquery.fancybox.pack.js"></script>
  

  
    <script type="text/javascript" src="/blog/lib/pjax/jquery.pjax.min.js"></script>
  

  
    <script type="text/javascript" src="/blog/lib/nprogress/nprogress.min.js"></script>
  


    <script type="text/javascript" src="/blog/js/src/even.js?v=2.9.0"></script>

  </body>
</html>
